package controllers

import auth.AuthTool._
import auth._
import dao._
import implicits.Implicits._
import models.Tables._
import org.joda.time.DateTime
import play.api.mvc.{AbstractController, ControllerComponents}
import shared.pojo.FreezerSet.FreezerSetData
import shared.pojo.Pojo._
import tool.Tool
import upickle.default._

import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global
import io.scalaland.chimney.dsl._

/**
 * Created by Administrator on 2019/8/7
 */
class FreezerSetController @Inject()(cc: ControllerComponents,
                                    )(
                                      implicit val authAction: AuthActionWrapper,
                                      implicit val freezerSetDao: FreezerSetDao,
                                    ) extends
  AbstractController(cc) {

  def insertOrUpdate = adminAuth.async { implicit request =>
    val data = Tool.transformRq.transform(reader[FreezerSetData])
    val row = data.transformInto[FreezerSetRow]
    freezerSetDao.insertOrUpdate(row).map { dbUser =>
      val response = ValidData(valid = true)
      Ok(writeJs(response))
    }
  }

  def get = Action.async { implicit request =>
    val data = Tool.transformRq.transform(reader[FreezerSetData])
    val row = data.transformInto[FreezerSetRow]
    freezerSetDao.selectOp(row).map { dbX =>
      Ok(writeJs(dbX))
    }
  }


}
